{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'weights': [{'data': [0.30717917, -0.76998611, 0.90056573, -0.0356172, 0.74494907, -0.57533464, -0.91858075, -0.20561108, -0.53373561, 0.68348145, -0.58583531, 0.48493907, -0.21569174, -0.63548696, 0.48707883, -0.86083584, 0.77067441, 0.9052888, 0.86228687, -0.16913809, -0.94203668, 0.96405497, -0.32072463, 0.41337439, -0.27624586, -0.9297882, 0.71011651, 0.31450702, 0.53136599, 0.10817448, 0.77018587, 0.80839523, -0.9791566, -0.85088653, -0.51074158, -0.7333905, 0.3958502, -0.20359023, 0.76624438, -0.63798498, -0.13500166, -0.96371359, 0.38287572, -0.0606187, -0.74355562, 0.7826741, 0.83640725, -0.85375801, -0.90910411, -0.12285421, 0.20344186, -0.37954593, 0.36381648, -0.5819737, 0.03920859, 0.13197766, -0.11766521, -0.72488769, -0.57291362, -0.73325622, -0.3554066, -0.53222577, 0.05499631, 0.13194232, -0.12354943, -0.35625475, 0.11928162, 0.73069116, 0.69456575, -0.78436567, -0.36736103, -0.18104268], 'shape': [3, 3, 2, 4]}, {'data': [-0.38753564, -0.46987287, -0.60787987, -0.13895705], 'shape': [4]}, {'data': [-0.74202342, -0.07768763, -0.16769214, 0.20544846, -0.63386391, -0.16417457, -0.73182259, 0.31323626, 0.61346464, -0.72371608, -0.29923053, 0.22903161, 0.10256058, 0.3849493, -0.90947957, -0.29489768, -0.91621686, -0.69903071, -0.32332886, -0.67344487, 0.52194947, -0.30679612, -0.47601763, -0.62862325, 0.80802829, -0.58504313, -0.30742911, -0.23486826, -0.89758393, 0.74174331, 0.32078472, 0.70913156, -0.97808403, 0.60189362, -0.22881613, -0.06955834, -0.52206554, -0.39959651, -0.91622249, 0.16154937, -0.21191454, 0.82337215, -0.65489982, -0.30402974, 0.67758845, -0.43125868, 0.21965887, -0.09193696, -0.10163649, -0.5952181, -0.8154284, 0.5029318, 0.77524911, 0.62422592, 0.62260109, -0.09107489, 0.76360284, 0.47265932, 0.62113081, -0.50454929, -0.27021405, 0.49274925, 0.64305497, -0.29005809, -0.75216247, 0.75891802, 0.01183237, -0.18396672, 0.7682978, 0.764241, 0.90639802, 0.87285293, -0.292238, 0.16788006, -0.44774071, 0.67919563, 0.56661355, 0.8675487, -0.01160559, -0.25210802, 0.16566883, -0.50936157, 0.62063154, -0.32465021, -0.07114311, -0.82361269, 0.33106663, -0.01690254, -0.76137972, -0.49114637, 0.10608759, -0.64149185, 0.23489321, 0.65885295, -0.47562328, 0.26910266, 0.93550511, -0.57713379, 0.98501453, -0.4059571, -0.32588248, 0.84951791, -0.58915493, 0.37833081, -0.75307471, 0.71141124, 0.04547021, 0.39832694, -0.66565676, 0.53114172, -0.41029273, -0.52664882, 0.86064757, 0.32794956, -0.19708224, -0.09552605, -0.39136149, 0.78546529, -0.26726936, -0.02015412, -0.95188952, -0.58074168, 0.78810372, -0.09243264, 0.32035388, 0.07065095, 0.04541633, 0.99799027, 0.58311614, -0.70813106, -0.1047838, -0.83894714, -0.59822383, 0.20910538, 0.82495609, 0.10438003, 0.6920462, -0.09130816, 0.88489557, 0.73061729, 0.24448579, -0.41562414, -0.39771377, -0.64723559], 'shape': [3, 3, 4, 4]}, {'data': [0.1956116, -0.12813242, -0.96626001, 0.19337492], 'shape': [4]}, {'data': [-0.92209672, 0.71299172, 0.49300062, 0.72785579, 0.11996881, -0.83903385, -0.5367274, -0.51547196, 0.23100003, 0.21421819, -0.79163594, -0.14830398, 0.30984568, 0.74277888, -0.12302193, 0.42758265, -0.88227586, 0.81857139, 0.04363382, 0.45485899, -0.00731125, -0.74489547, -0.36822924, 0.3248054, -0.38875841, -0.55621489, -0.54285855, 0.68565455, 0.35078536, -0.31275261, 0.59140089, 0.95999013, 0.13636859, -0.58843976, -0.506667, -0.20873578, 0.54896934, 0.65317255, 0.12894261, 0.18009365, -0.1609796, 0.2087984, 0.66624462, 0.34730735, -0.38473326, -0.88354016, -0.32846825, -0.51532434, 0.47924716, -0.36064687, 0.09068954, -0.22142383, 0.09128434, 0.20263136, 0.20808747, 0.58224751, -0.16406354, -0.92503631, -0.67880595, -0.21284573, 0.96086117, 0.53608922, -0.03863406, -0.47345639, -0.40940764, 0.62031463, -0.87308544, -0.69540482, -0.02446451, 0.76284273, -0.92822834, 0.5571065, -0.65499028, -0.91835582, 0.81549052, 0.99643052, 0.11576935, -0.75165237, 0.07522943, 0.96998338, -0.80408957, -0.08066096, -0.64408765, 0.16070239, -0.48651818, -0.09818037, -0.191651, -0.96156565, -0.23820938, 0.26042664, 0.08530711, -0.66443709, 0.45851693, -0.82469248, 0.3127677, -0.25369817, 0.76171767, 0.55121499, 0.56600928, -0.85706044, 0.68790436, -0.2838193, 0.58159968, 0.82008702, -0.02847436, 0.58815274, -0.22114462, 0.04917316, 0.52932814, -0.35907408, -0.46316143, 0.49396729, -0.85279284, -0.55267535, -0.69574755, -0.17815679, 0.47799466, 0.85872499, 0.12038387, -0.51520863, 0.20448435, -0.02502506, -0.65496124, 0.23958519, -0.6546914, -0.65169642, -0.69995072, -0.05462618, -0.23299863, 0.46497433, 0.2854986, -0.3111654, 0.18008997, -0.10050491, 0.30394317, 0.26553491, -0.96074652, -0.54241814, 0.19517799, -0.84839357, 0.07740009, 0.25061518, -0.69054067, -0.10658937], 'shape': [3, 3, 4, 4]}, {'data': [0.31842886, -0.85839736, -0.0590418, 0.68597037], 'shape': [4]}], 'input': {'data': [0.30717917, -0.76998611, 0.90056573, -0.0356172, 0.74494907, -0.57533464, -0.91858075, -0.20561108, -0.53373561, 0.68348145, -0.58583531, 0.48493907, -0.21569174, -0.63548696, 0.48707883, -0.86083584, 0.77067441, 0.9052888, 0.86228687, -0.16913809, -0.94203668, 0.96405497, -0.32072463, 0.41337439, -0.27624586, -0.9297882, 0.71011651, 0.31450702, 0.53136599, 0.10817448, 0.77018587, 0.80839523, -0.9791566, -0.85088653, -0.51074158, -0.7333905, 0.3958502, -0.20359023, 0.76624438, -0.63798498, -0.13500166, -0.96371359, 0.38287572, -0.0606187, -0.74355562, 0.7826741, 0.83640725, -0.85375801, -0.90910411, -0.12285421, 0.20344186, -0.37954593, 0.36381648, -0.5819737, 0.03920859, 0.13197766, -0.11766521, -0.72488769, -0.57291362, -0.73325622, -0.3554066, -0.53222577, 0.05499631, 0.13194232, -0.12354943, -0.35625475, 0.11928162, 0.73069116, 0.69456575, -0.78436567, -0.36736103, -0.18104268, 0.37417803, 0.40447057, -0.10760411, -0.15935584, 0.60526118, 0.07723513, 0.84700098, -0.87618493, -0.26483309, 0.94079754, 0.3982079, 0.78195116, -0.49289456, 0.45105421, -0.59301113, 0.07502365, -0.52611399, -0.12701568, 0.59604903, -0.38318167, 0.24303326, -0.12070376, 0.82664741, 0.31737181, 0.3072629, -0.28308443, 0.04588313, -0.90982538, -0.81138068, 0.84322439, -0.853911, 0.85883471, 0.43403027, 0.24462109, 0.50997943, -0.71666018, 0.5876444, 0.4508063, 0.08287856, -0.03483101, -0.04704478, -0.10793434, 0.63214012, -0.45129701, -0.87607598, 0.92059268, -0.08376374, 0.51578413, 0.36231721, 0.08355585, -0.73433502, -0.49358873, -0.11228881, 0.11749981, 0.62772871, -0.36434333], 'shape': [8, 8, 2]}, 'expected': {'data': [5.00916195, 0.0, 0.0, 0.0, 1.77027261, 3.24344349, 0.0, 3.31952095, 0.0, 2.15876007, 0.0, 0.0, 4.5092907, 0.18820691, 0.0, 0.0], 'shape': [2, 2, 4]}}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "conv_0 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_1 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_2 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = conv_0(input_layer)\n",
    "x = conv_1(x)\n",
    "output_layer = conv_2(x)\n",
    "model = Model(input=input_layer, output=output_layer)\n",
    "\n",
    "np.random.seed(1000)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(1000 + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "\n",
    "print({\n",
    "    'input': {'data': format_decimal(data_in.ravel().tolist()), 'shape': list(data_in_shape)},\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
